package com.xyz.study.design_mode.n_chain_of_responsibility;

import com.xyz.study.design_mode.n_chain_of_responsibility.mode.*;

/**
 * 访问数据结构：其二 ---- ChainOfResponsibility 责任链模式
 * 角色：Client 客户端
 * 将多个处理器组成一个责任链，按照顺序找出谁来负责处理
 * @author XYZ
 * @version 1.0.0
 * @since 2020/6/22
 */
public class ChainOfResponsibilityMain {
    public static final Handler handler = new ConcreteHandlerA();

    static {
        handler.next(new ConcreteHandlerB());
        handler.next.next(new ConcreteHandlerC());
    }
    public static void main(String[] args) {
        process("A");
        process("B");
        process("C");
        process("D");
    }

    /**
     * 对外开放
     * @param something something
     */
    private static void process(String something) {
        process(something, handler);
    }
    /**
     * 对内处理
     * @param something something
     * @param handler handler
     */
    private static void process(String something, Handler handler) {
        if (handler == null) {
            return;
        }
        if (handler.support(something)) {
            System.out.println("process object's class name is " + handler.getClass().getSimpleName());
            handler.handle();
        } else {
            process(something, handler.next);
        }
    }
}
